import QtQuick
import Qt3D.Input 2.7

Window {
    id: window

    flags: Qt.Window | Qt.FramelessWindowHint

    property int minWidth: 800
    property int minHeight: 600

    property int edgeSize: 20

    property bool isLeftEdge: false
    property bool isRightEdge: false
    property bool isTopEdge: false
    property bool isBottomEdge: false
    property bool isLeftTopCorner: false
    property bool isRightTopCorner: false
    property bool isLeftBottomCorner: false
    property bool isRightBottomCorner: false
    property bool isInsideWindow: false

    property int cursorShape: Qt.ArrowCursor

    signal windowMoved

    MouseArea {
        id: resizeArea

        anchors.fill: parent
        hoverEnabled: true
        cursorShape: window.cursorShape
        smooth: true

        property real startMouseX: 0
        property real startMouseY: 0
        property real startWindowX: 0
        property real startWindowY: 0
        property real startWidth: 0
        property real startHeight: 0

        property bool isResizing: false

        onPositionChanged: function (mouse) {
            if (resizeArea.isResizing === false) {
                const x = mouse.x;
                const y = mouse.y;
                const w = window.width;
                const h = window.height;

                // 重置状态
                resetEdgeStates();

                // 检测边缘和角落
                window.isLeftEdge = x < window.edgeSize;
                window.isRightEdge = x > w - window.edgeSize;
                window.isTopEdge = y < window.edgeSize;
                window.isBottomEdge = y > h - window.edgeSize;

                // 检测角落
                window.isLeftTopCorner = window.isLeftEdge && window.isTopEdge;
                window.isRightTopCorner = window.isRightEdge && window.isTopEdge;
                window.isLeftBottomCorner = window.isLeftEdge && window.isBottomEdge;
                window.isRightBottomCorner = window.isRightEdge && window.isBottomEdge;

                // 设置鼠标样式
                updateCursorShape();
            }

            if (resizeArea.pressed) {
                handleResize(mouse);
            }
        }

        onPressed: function (mouse) {
            resizeArea.startMouseX = mouse.x + window.x;
            resizeArea.startMouseY = mouse.y + window.y;
            resizeArea.startWindowX = window.x;
            resizeArea.startWindowY = window.y;
            resizeArea.startWidth = window.width;
            resizeArea.startHeight = window.height;

            if (cursorShape !== Qt.ArrowCursor) {
                resizeArea.isResizing = true;
            }
        }

        onReleased: function (mouse) {
            resizeArea.isResizing = false;
        }

        function resetEdgeStates() {
            window.isLeftEdge = false;
            window.isRightEdge = false;
            window.isTopEdge = false;
            window.isBottomEdge = false;
            window.isLeftTopCorner = false;
            window.isRightTopCorner = false;
            window.isLeftBottomCorner = false;
            window.isRightBottomCorner = false;
        }

        function updateCursorShape() {
            if (window.isLeftTopCorner || window.isRightBottomCorner) {
                window.cursorShape = Qt.SizeFDiagCursor;
            } else if (window.isRightTopCorner || window.isLeftBottomCorner) {
                window.cursorShape = Qt.SizeBDiagCursor;
            } else if (window.isLeftEdge || window.isRightEdge) {
                window.cursorShape = Qt.SizeHorCursor;
            } else if (window.isTopEdge || window.isBottomEdge) {
                window.cursorShape = Qt.SizeVerCursor;
            } else {
                window.cursorShape = Qt.ArrowCursor;
            }
        }

        function handleResize(mouse) {
            const deltaX = window.x + mouse.x - resizeArea.startMouseX;
            const deltaY = window.y + mouse.y - resizeArea.startMouseY;

            if (window.isLeftTopCorner) {
                window.x = resizeArea.startWindowX + deltaX;
                window.width = resizeArea.startWidth - deltaX > window.minWidth ? resizeArea.startWidth - deltaX : window.minWidth;
                window.y = resizeArea.startWindowY + deltaY;
                window.height = resizeArea.startHeight - deltaY > window.minHeight ? resizeArea.startHeight - deltaY : window.minHeight;
            } else if (window.isRightTopCorner) {
                window.width = resizeArea.startWidth + deltaX > window.minWidth ? resizeArea.startWidth + deltaX : window.minWidth;
                window.y = resizeArea.startWindowY + deltaY;
                window.height = resizeArea.startHeight - deltaY > window.minHeight ? resizeArea.startHeight - deltaY : window.minHeight;
            } else if (window.isLeftBottomCorner) {
                window.x = resizeArea.startWindowX + deltaX;
                window.width = resizeArea.startWidth - deltaX > window.minWidth ? resizeArea.startWidth - deltaX : window.minWidth;
                window.height = resizeArea.startHeight + deltaY > window.minHeight ? resizeArea.startHeight + deltaY : window.minHeight;
            } else if (window.isRightBottomCorner) {
                window.width = resizeArea.startWidth + deltaX > window.minWidth ? resizeArea.startWidth + deltaX : window.minWidth;
                window.height = resizeArea.startHeight + deltaY > window.minHeight ? resizeArea.startHeight + deltaY : window.minHeight;
            } else if (window.isLeftEdge) {
                window.x = resizeArea.startWindowX + deltaX;
                window.width = resizeArea.startWidth - deltaX > window.minWidth ? resizeArea.startWidth - deltaX : window.minWidth;
            } else if (window.isRightEdge) {
                window.width = resizeArea.startWidth + deltaX > window.minWidth ? resizeArea.startWidth + deltaX : window.minWidth;
            } else if (window.isTopEdge) {
                window.y = resizeArea.startWindowY + deltaY;
                window.height = resizeArea.startHeight - deltaY > window.minHeight ? resizeArea.startHeight - deltaY : window.minHeight;
            } else if (window.isBottomEdge) {
                window.height = resizeArea.startHeight + deltaY > window.minHeight ? resizeArea.startHeight + deltaY : window.minHeight;
            }
        }


        // 拖动区域
        MouseArea {
            id: dragArea

            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.margins: window.edgeSize

            hoverEnabled: true
            property var startPos: Qt.vector2d(0, 0)

            onPressed: function (mouse) {
                dragArea.startPos = Qt.vector2d(mouse.x, mouse.y);
            }

            onPositionChanged: function (mouse) {
                if (dragArea.pressed) {
                    window.windowMoved();
                    var delta = Qt.vector2d(mouse.x - startPos.x, mouse.y - startPos.y);
                    window.x += delta.x;
                    window.y += delta.y;
                }
            }
        }
    }
}
